package com.isunland.managesystem.ui;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import com.google.gson.Gson;
import com.isunland.managesystem.adapter.MessageReplyAdapter;
import com.isunland.managesystem.base.BaseListFragment;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.entity.MessageDetailOriginal;
import com.isunland.managesystem.entity.ReplyList;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class MessageReplyListFragment extends BaseListFragment {

  private static final String EXTRA_ID = "com.isunland.managesystem.ui.extra_id";
  private ArrayList<ReplyList> mReplyList;
  private MessageReplyAdapter mAdapter;
  private long mId;

  @Override public String getUrl() {
    // Auto-generated method stub
    return ApiConst.URL_MESSAGE_DETAIL;
  }

  @Override public void onCreate(Bundle savedInstanceState) {
    mId = getArguments().getLong(EXTRA_ID);
    super.onCreate(savedInstanceState);
  }

  @Override public HashMap<String, String> getparameters() {
    HashMap<String, String> params = new HashMap<String, String>();
    params.put("messageId", mId + "");

    return params;
  }

  @Override public void resolveResponse(String arg0) {

    MessageDetailOriginal messageDetailOriginal =
        new Gson().fromJson(arg0, MessageDetailOriginal.class);

    List<ReplyList> replyLists = messageDetailOriginal.getReplyList();

    setAdapter(replyLists);
  }

  private void setAdapter(List<ReplyList> replyLists) {
    if (mReplyList == null) {
      mReplyList = new ArrayList<ReplyList>();
    }
    mReplyList.clear();
    mReplyList.addAll(replyLists);

    if (mAdapter == null) {
      mAdapter = new MessageReplyAdapter(getActivity(), mReplyList);
      setListAdapter(mAdapter);
    }
    ((MessageReplyAdapter) getListAdapter()).notifyDataSetChanged();
  }

  public Fragment newInstance(long id) {
    Bundle args = new Bundle();
    args.putLong(EXTRA_ID, id);
    MessageReplyListFragment fragment = new MessageReplyListFragment();
    fragment.setArguments(args);
    return fragment;
  }
}
